package edu.gduf.service.impl;
/*
@author qw
@date 2020/10/10 - 22:57
**/

import edu.gduf.dao.CooperationDao;
import edu.gduf.domain.Cooperation;
import edu.gduf.domain.ResultInfo;
import edu.gduf.service.CooperationService;
import edu.gduf.utils.DateUtil;
import edu.gduf.utils.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CooperationServiceImpl implements CooperationService {

    /**
     * 声明分页一页的数据量为10条
     */
    private Integer pageSize = 10;

    private CooperationDao cooperationDao;

    @Autowired
    public void setCooperationDao(CooperationDao cooperationDao) {
        this.cooperationDao = cooperationDao;
    }

    /**
     * 组队信息发布（添加组队信息）
     *
     * @param cooperation 前端表单提交cooperation数据
     * @param httpSession httpSession域
     * @return
     */
    @Override
    public ResultInfo addCooperation(Cooperation cooperation, HttpSession httpSession) {
        //从httpSession域中取出uid值
        Integer uid = (Integer) httpSession.getAttribute("uid");

        //设置表单不能够直接填写的值
        cooperation.setUid(uid);
        cooperation.setPublish_time(DateUtil.getCurrentTime());
        cooperation.setStatus(1);
        //发布时设置默认浏览量view为0
        cooperation.setView(0);



        //获取需要用户填写的信息，进行敏感词检测
        boolean rightTitle = SensitiveWordsUtil.isContaintBadWord(cooperation.getTitle(), 1);
        boolean rightContent = SensitiveWordsUtil.isContaintBadWord(cooperation.getContent(), 1);
        boolean rightDemand = SensitiveWordsUtil.isContaintBadWord(cooperation.getDemand(), 1);

        if (rightTitle) {
            return ResultInfo.warnInfo("组队信息发布异常！组队标题存在敏感词汇！");
        } else if (rightContent) {
            return ResultInfo.warnInfo("组队信息发布异常！组队内容存在敏感词汇！");
        }
        else if (rightDemand) {
            return ResultInfo.warnInfo("组队信息发布异常！组队要求存在敏感词汇！");
        }
        else {
            //调用dao层接口进行数据添加
            int count = cooperationDao.addCooperation(cooperation);
            //0-失败，1-成功
            if (count == 1) {
                return ResultInfo.successInfo("组队信息发布成功！");
            } else {
                return ResultInfo.failInfo("组队信息发布失败！数据存在错误，请进行检查！");
            }
        }
    }

    /**
     * 组队信息删除
     *
     * @param coid 组队信息主键coid
     * @return json数据
     */
    @Override
    public ResultInfo deleteCooperation(Integer coid) {

        //调用dao层接口进行数据删除
        int count = cooperationDao.deleteCooperation(coid);

        //0-失败，1-成功
        if (count == 1) {
            return ResultInfo.successInfo("组队信息删除成功！");
        } else {
            return ResultInfo.failInfo("组队信息删除失败");
        }
    }

    /**
     * 组队信息批量删除
     *
     * @param coidArray 从复选框中选择需要删除的组队信息主键数组
     * @return json数据
     */
    @Override
    public ResultInfo BatchDelete(Integer[] coidArray) {
        int count = 0;//统计成功删除的条数
        for (int i = 0; i < coidArray.length; i++) {
            int x = cooperationDao.deleteCooperation(coidArray[i]);//0-失败，1-成功
            count = count + x;
        }

        //成功删除的条数count与数组长度相等 则全部删除成功
        if (count == coidArray.length) {
            return ResultInfo.successInfo("成功删除" + count + "条组队信息");
        } else if (count < coidArray.length) {
            return ResultInfo.failInfo("组队信息部分删除失败,仅成功删除了" + count + "条");
        } else {
            return ResultInfo.failInfo("组队信息删除失败");
        }
    }

    /**
     * 组队信息更新
     *
     * @param cooperation 前端表单提交cooperation修改的数据
     * @return json数据
     */
    @Override
    public ResultInfo updateCooperation(Cooperation cooperation) {
        //获取需要用户填写的信息，进行敏感词检测
        boolean rightTitle = SensitiveWordsUtil.isContaintBadWord(cooperation.getTitle(), 2);
        boolean rightContent = SensitiveWordsUtil.isContaintBadWord(cooperation.getContent(), 2);
        boolean rightDemand = SensitiveWordsUtil.isContaintBadWord(cooperation.getDemand(), 2);

        if (rightTitle) {
            return ResultInfo.failInfo("组队信息更新失败！组队标题存在敏感词汇！");
        } else if (rightContent) {
            return ResultInfo.failInfo("组队信息更新失败！组队内容存在敏感词汇！");
        } else if (rightDemand) {
            return ResultInfo.failInfo("组队信息更新失败！组队要求存在敏感词汇！");
        } else {
            //调用dao层接口进行数据修改
            int count = cooperationDao.updateCooperation(cooperation);

            //0-失败，1-成功
            if (count == 1) {
                return ResultInfo.successInfo("组队信息更新成功！");
            } else {
                return ResultInfo.failInfo("组队信息更新失败！数据存在错误，请进行检查！");
            }
        }

    }

    /**
     * 组队信息查询（单条）
     *
     * @param coid 组队信息表主键
     * @return 组队信息详细内容页面
     */
    @Override
    public Cooperation selectCooperationByCoid(Integer coid) {
        //调用dao层接口进行数据查询
        Cooperation cooperation= cooperationDao.selectCooperationByCoid(coid);

        if (cooperation != null) {
            int views=cooperation.getView() + 1;
            //调用dao层接口进行浏览量view的更新
            int i = cooperationDao.addViews(coid, views);
            cooperation.setView(views);
            return cooperation;
        } else {
            return null;
        }
        //0-失败，1-成功
/*        if (cooperation != null) {
            int views=cooperation.getView() + 1;
            //调用dao层接口进行浏览量view的更新
            int i = cooperationDao.addViews(coid, views);
            cooperation.setView(views);
            return ResultInfo.successInfo("组队信息查询成功！", cooperation);
        } else {
            return ResultInfo.failInfo("组队信息查询失败");
        }*/
    }


    /**
     * 组队信息查询（全部-分页）
     *
     * @param currentPageIndex 当前页数下标
     * @return 键值对为页数"pageCount"和符合条件的组队信息列表"cooperationList"的map
     */
    @Override
    public ResultInfo selectCooperationList(Integer currentPageIndex) {
        //查询下标值
        Integer startIndex = (currentPageIndex - 1) * pageSize;

        //调用dao层接口进行数据查询
        List<Cooperation> cooperationList = cooperationDao.selectCooperationList(startIndex, pageSize);

        for (Cooperation c : cooperationList) {
            System.out.println(c);
        }

        //查询所有组队信息的数量
        Integer totalNumber = cooperationDao.totalNumber();

        //页数
        Integer pageCount = (totalNumber % pageSize) != 0 ? (totalNumber / pageSize) + 1 : (totalNumber / pageSize);

        //将数据以键值对的形式存入到map中
        Map<String, Object> map = new HashMap<>();

        map.put("pageCount", pageCount);
        map.put("cooperationList", cooperationList);

        if (cooperationList == null || cooperationList.size() == 0) {
            return ResultInfo.successInfo("查询结果为0条");
        } else {
            return ResultInfo.successInfo("组队信息查询成功！", map);
        }
    }


    /**
     * 组队信息查询（根据分类查询）
     * @param cid 查询条件（分类表中的主键id）
     * @return 键值对为页数"pageCount"和符合条件的组队信息列表"cooperationList"的map
     */
    @Override

    public ResultInfo selectCooperationByCid(Integer cid, Integer currentPageIndex) {
        //查询下标值
        Integer startIndex = (currentPageIndex - 1) * pageSize;

        //调用dao层接口进行数据查询
        List<Cooperation> cooperationList = cooperationDao.selectCooperationByCid(cid, startIndex, pageSize);

        //模糊查询的数据数量
        Integer cidCount = cooperationDao.countByCid(cid);

        //页数
        Integer pageCount = (cidCount % pageSize) != 0 ? (cidCount / pageSize) + 1 : (cidCount / pageSize);

        //将数据以键值对的形式存入到map中
        Map<String, Object> map = new HashMap<>();

        map.put("pageCount", pageCount);
        map.put("cooperationList", cooperationList);

        if (cooperationList == null || cooperationList.size() == 0) {
            return ResultInfo.successInfo("查询结果为0条");
        } else {
            return ResultInfo.successInfo("组队信息查询成功！", map);
        }
    }


    /**
     * 组队信息查询（根据标题或要求--模糊）
     * @param str 查询条件
     * @param currentPageIndex 当前页面值下标
     * @return 键值对为页数"pageCount"和符合条件的组队信息列表"cooperationList"的map
     */
    @Override
    public ResultInfo selectCooperationByCondition(String str, Integer currentPageIndex) {
        //查询下标值
        Integer startIndex = (currentPageIndex - 1) * pageSize;

        //调用dao层接口进行数据查询
        List<Cooperation> cooperationList = cooperationDao.selectCooperationByCondition(str, startIndex, pageSize);

        //模糊查询的数据数量
        Integer cidCount = cooperationDao.countByCondition(str);

        //页数
        Integer pageCount = (cidCount % pageSize) != 0 ? (cidCount / pageSize) + 1 : (cidCount / pageSize);

        //将数据以键值对的形式存入到map中
        Map<String, Object> map = new HashMap<>();

        map.put("pageCount", pageCount);
        map.put("cooperationList", cooperationList);

        if (cooperationList == null || cooperationList.size() == 0) {
            return ResultInfo.successInfo("查询结果为0条");
        } else {
            return ResultInfo.successInfo("组队信息查询成功！", map);
        }

    }

    /**
     * 更新组队状态
     *
     * @param coid 组队信息编号（主键）
     * @return json
     */
    @Override
    public ResultInfo updateStatus(Integer coid) {
        //调用dao层接口 修改组队信息状态status 将status置为0
        int i = cooperationDao.updateStatus(coid);
        return ResultInfo.successInfo("该队伍已停止组队");
    }

    /**
     * 更新组队状态（根据组队的截止时间 判断是否停止组队 进行状态修改）
     *
     * @param timeout 时间标识符
     * @param coid    组队信息编号（主键）
     * @return json
     */
    @Override
    public ResultInfo updateStatus(Boolean timeout, Integer coid) {
        if (timeout) {
            Integer i = cooperationDao.updateStatus(coid);
            if (i == 1) {
                return ResultInfo.successInfo("组队已截止！");
            } else {
                return ResultInfo.failInfo("内部数据错误，请联系管理员！");
            }
        } else {
            return ResultInfo.failInfo("该队伍正在组队！");
        }
    }


    /**
     * 更新组队人数
     *
     * @param coid 组队信息编号（主键）
     * @return json
     */
    @Override
    public ResultInfo updatePopulation(Integer coid, String username) {
        //调用dao层查询coid对应的cooperation对象
        Cooperation cooperation = cooperationDao.selectCooperationByCoid(coid);

        //需更新的人数
        int population = cooperation.getPopulation() - 1;

        //获取list
        String list = cooperation.getList();
        //定义一个newList
        String newList = list + "," + username;


        //判断是否能加入该队伍
        if (population >= 0) {
            //最后一个人加入队伍后，队伍状态置为0
            if (population == 0) {
                //调用dao层接口 修改组队信息状态status 将status置为0
                int j = cooperationDao.updateStatus(coid);
            }
            //更新组队人数
            int i = cooperationDao.updatePopulation(coid);
            if (i == 1) {
                return ResultInfo.successInfo("加入队伍成功,当前仍需" + population + "人", cooperation);
            } else {
                return ResultInfo.failInfo("加入该队伍失败");
            }
        } else {
            return ResultInfo.failInfo("该队伍人数已满，已停止组队");
        }
    }

    @Override
    public ResultInfo selectCooperationByUid(HttpSession session, Integer currentPageIndex) {
        Integer uid= (Integer) session.getAttribute("uid");

        //查询下标值
        Integer startIndex = (currentPageIndex - 1) * pageSize;

        //调用dao层接口进行数据查询
        List<Cooperation> cooperationList = cooperationDao.selectCooperationByUid(uid, startIndex, pageSize);

        Integer uidCount = cooperationDao.countByUid(uid);

        //页数
        Integer pageCount = (uidCount % pageSize) != 0 ? (uidCount / pageSize) + 1 : (uidCount / pageSize);

        //将数据以键值对的形式存入到map中
        Map<String, Object> map = new HashMap<>();

        map.put("pageCount", pageCount);
        map.put("list", cooperationList);

        if (cooperationList == null || cooperationList.size() == 0) {
            return ResultInfo.successInfo("我的发布信息为0条");
        } else {
            return ResultInfo.successInfo("我的发布如下！", map);
        }
    }

    /**
     * 统计组队信息作者发布数
     * @param uid
     * @return
     */
    @Override
    public Integer countByAuthor(Integer uid) {
        Integer uidCount = cooperationDao.countByUid(uid);
        return uidCount;
    }


}
